package com.codepilot.server.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.codepilot.server.model.dto.UserDTO;
import com.codepilot.server.model.entity.User;
import com.codepilot.server.mapper.UserMapper;

/**
 * 自定义UserDetailsService实现类，替换Spring Security的默认实现
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 查询用户信息
        User foundUser = userMapper.findByUsername(username);
        if (foundUser == null) {
            throw new UsernameNotFoundException("用户不存在");
        }

        // 把user数据封装成UserDetails返回
        return new UserDTO(foundUser);
    }
    
}
